//
//  MainViewController.swift
//  我的微博
//
//  Created by yz on 15/9/6.
//  Copyright © 2015年 yz. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
       
    }
    
    //  添加所有控制器
    private func addChildViewControllers(){
        addChildViewController(HomeTableViewController(), titile: "首页", imageName: "tabbar_home")
        addChildViewController(MessagesTableViewController(), titile: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), titile: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), titile: "我", imageName: "tabbar_profile")
    }
    // 添加子控制器
    private func addChildViewController(childController: UIViewController, titile:String,imageName:String){
        
        childController.title = titile
        childController.tabBarItem.image = UIImage(named: imageName)
        let nvc = UINavigationController(rootViewController: childController)
        addChildViewController(nvc)
    }
    
    /*:
        ComposeButton添加时不会马上创建,故无法设置frame
        设置frame方法需放在viewWillAppear中
    */
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setComposeButton()
    }
    
    // 懒加载
    lazy private var composeButton:UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        self.tabBar.addSubview(button)
        button.addTarget(self, action:"clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    private func setComposeButton(){
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composeButton.frame = CGRectOffset(rect, 2 * w, 0)
    }
    func clickComposedButton(){
        print(__FUNCTION__)
    }

}






